package com.kkb.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kkb.mapper.BehospitalMapper;
import com.kkb.mapper.HosregisterMapper;
import com.kkb.pojo.Behospital;
import com.kkb.pojo.BehospitalExample;
import com.kkb.pojo.Hosregister;
import com.kkb.vo.BehospitalVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author:CoolTiger
 * @title: BehospitalService
 * @projectName hospital1.0
 * @description: TODO
 * @date:2022/07/23/21:15:24
 **/
@Service
public class BehospitalService {
    @Autowired
    private BehospitalMapper behospitalMapper;
    @Autowired
    private HosregisterMapper hosregisterMapper;

    /**
     * TODO 分页多条件查询
     *
     * @param pageNum
     * @param pageSize
     * @param vo
     * @return
     */
    @Transactional(propagation = Propagation.REQUIRED, readOnly = true)
    public PageInfo<Behospital> queryAll(Integer pageNum, Integer pageSize, BehospitalVo vo) {
        //1.创建多条件查询容器
        BehospitalExample example = new BehospitalExample();
        BehospitalExample.Criteria criteria = example.createCriteria();
        if (vo != null) {
            //多条件分类
            //1.病历号
            if (vo.getHosRRecode() != null || !"".equals(vo.getHosRRecode().trim())) {
                criteria.andHosrRecodeLike("%" + vo.getHosRRecode() + "%");
            }
            //2.主治医生
            if (vo.getDoctorName() != null || !"".equals(vo.getDoctorName().trim())) {
                criteria.andDNameLike("%" + vo.getDoctorName() + "%");
            }
            //3.科室
            if (vo.getDoctorKeshi() != null || !"".equals(vo.getDoctorKeshi().trim())) {
                criteria.andDKeshiLike("%"+vo.getDoctorKeshi()+"%");
            }
            //4.住院时间
            if (vo.getBeginTime() != null){
                criteria.andBehTimeGreaterThanOrEqualTo(vo.getBeginTime());
            }
            if (vo.getEndTime() != null){
                criteria.andBehTimeLessThanOrEqualTo(vo.getEndTime());
            }
        }


        //2.多条件查询+分页
        PageHelper.startPage(pageNum, pageSize);//分页
        List<Behospital> behospitals = behospitalMapper.selectByExample(example);//多条件查询
        for (Behospital behospital : behospitals) {
            //3.查询挂号信息:通过病历号
            Hosregister hosregister = hosregisterMapper.queryByRecode(behospital.getHosrRecode());
            behospital.setHosregister(hosregister);
        }
        return new PageInfo<>(behospitals);
    }

    /**
     * TODO 添加住院
     *
     * @param behospital
     * @return
     */
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = {Exception.class})
    public int addBehospital(Behospital behospital) {

        return behospitalMapper.insertSelective(behospital);
    }

    /**
     * TODO 退院
     *
     * @param behospitalId
     * @return
     */
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = {Exception.class})
    public int deleteBehospital(Integer behospitalId) {
        Behospital behospital = behospitalMapper.selectByPrimaryKey(behospitalId);
        behospital.setBehState(1);
        return behospitalMapper.updateByPrimaryKeySelective(behospital);
    }

    /**
     * TODO 住院信息回显
     *
     * @param behospitalId
     * @return
     */
    @Transactional(propagation = Propagation.REQUIRED, readOnly = true)
    public Behospital queryById(Integer behospitalId) {
        Behospital behospital = behospitalMapper.selectByPrimaryKey(behospitalId);
        return behospital;
    }

    /**
     * TODO 更新住院信息
     *
     * @param behospital
     * @return
     */
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = {Exception.class})
    public int updateBehospital(Behospital behospital) {
        return behospitalMapper.updateByPrimaryKeySelective(behospital);
    }
}
